/**
 * 
 * If you have edited the file, please update
 * all the last-modified fields manually. Thanks!
 *
 * @last-modified by: 
 * @last-modified date: 
 * @last-modified time: 
 *
 * @author Swee Zhi
 * @date-created Sep 6, 2011    @time-created 9:55:00 PM
 *
 * @project: 
 * @module: 
 * @institution: 
 *
 */
package main;

import util.enumeration.Country;
import exception.ExistException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class LoadLocaleIso {
    BufferedReader keyboardReader = null;
   

    /** Creates a new instance of Main */
    public LoadLocaleIso() {
        keyboardReader = new BufferedReader (new InputStreamReader(System.in));
    }

    public static void main(String[] args) {
        LoadLocaleIso client = new LoadLocaleIso();
        client.displayMenu();
    }


    private void displayMenu() {
        String userInput = "";
        char quitMain = ' ';

        try {
            do {
                userInput = "";
                quitMain = ' ';

                System.out.println("==========================================");
                System.out.println("   LOAD LOCALE COUNTRIES AND LANGUAGES     ");
                System.out.println("==========================================\n");
                System.out.println("Please select:\n");
                System.out.println("a.\tLoad all country ISO codes");
                System.out.println("b.\tLoad all language ISO codes");
                System.out.println("c.\tLoad installed languages");
                System.out.println("d.\tSee installed name and respective locale CSV");
                System.out.println("e.\tSee installed name and respective locale CSV CAPS");
                System.out.println("f.\tSee installed locale codes only");
                System.out.println("g.\tSee installed display names only");
                System.out.println("h.\tLoad countries only CAPS");
                System.out.println("i.\tLoad countries only");
                System.out.println("q.\tExit Application\n");
                
                do {
                    System.out.print("Choice: ");
                    // get choice from console
                    userInput = keyboardReader.readLine().toLowerCase().trim();

                } while (!(userInput.equalsIgnoreCase("a") || userInput.equalsIgnoreCase("b") ||
                         userInput.equalsIgnoreCase("c") || userInput.equalsIgnoreCase("d") ||
                         userInput.equalsIgnoreCase("e") || userInput.equalsIgnoreCase("f") ||
                         userInput.equalsIgnoreCase("g") || userInput.equalsIgnoreCase("h") ||
                         userInput.equalsIgnoreCase("i") || 
                         userInput.equalsIgnoreCase("q")));

                if (userInput.equalsIgnoreCase("q")) {
                        quitMain = 'q';
                }
                else
                {
                    char choice = userInput.charAt(0);
                    switch (choice) {
                        case 'a':  loadCountryIsoCodes();       break;
                        case 'b':  loadLanguageIsoCodes();    break;
                        case 'c':  loadInstalledLanguages();         break;
                        case 'd':  loadInstalledLocaleCSVFormat();      break;
                        case 'e':  loadInstalledLocaleCSVFormatCAPS();        break;
                        case 'f':  loadInstalledLocaleISOCodesJavaStyle();     break;
                        case 'g':  loadDisplayNamesOnly();                     break;
                        case 'h':  loadCountriesOnlyCAPS(); break;
                        case 'i':  loadCountriesOnly(); break; 
                    }

                }

                System.out.println("\n");
                
            } while (quitMain != 'q');

            if (quitMain == 'q') {
                System.out.println("\nTerminated. Exiting...\n");
            }
        }
        catch (IOException ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
        catch (Exception ex) {
            System.out.println("ERROR: " + ex.getMessage());
        }
    }

    public void loadCountryIsoCodes() {
        for (String l : Locale.getISOCountries()) {
            System.out.println(l + ",");
        }
//            do {
//                System.out.print("\nEnter Desired Username: ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim().toLowerCase();
//                username = userInput;
//            } while (userInput.isEmpty());
//
//            do {
//                System.out.print("Enter Password: ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim();
//                password = userInput;
//            } while (userInput.isEmpty());
//            
//            do {
//                System.out.print("\nEnter Last Name: ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim().toUpperCase();
//                lastName = userInput;
//            } while (userInput.isEmpty());
//
//            do {
//                System.out.print("Enter First Name: ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim().toUpperCase();
//                firstName = userInput;
//            } while (userInput.isEmpty());
//
//            do {
//                System.out.print("Enter Gender (M/F): ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim().toUpperCase();
//                gender = userInput.charAt(0);
//            } while (userInput.isEmpty());
//            
//            do {
//                System.out.print("Enter Birthdate: ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim().toUpperCase();
//                SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
//                birthdate = sdf.parse(userInput);
//            } while (userInput.isEmpty());
//            
//            do {
//                System.out.print("Enter Country: ");
//                userInput = "";
//                userInput = keyboardReader.readLine().trim().toUpperCase();
//                if (!Country.valueOf(userInput).equals((Object)userInput)) {
//                    country = Country.valueOf(userInput);
//                }
//            } while (userInput.isEmpty());

    }
    
    public void loadLanguageIsoCodes() {
        for (String l : Locale.getISOLanguages()) {
            System.out.println(l + ",");
        }
    }
    
    public void loadInstalledLanguages() {
        for (Locale l : Locale.getAvailableLocales()) {
            System.out.println("Display Country:\t" + l.getDisplayCountry() + "(" + l.getCountry() + ")");
            System.out.println("Display Language:\t" + l.getDisplayLanguage() + "(" + l.getLanguage() + ")");
            System.out.println("Display Name:\t\t" + l.getDisplayName());
        }
    }
    
    public void loadInstalledLocaleCSVFormat() {
        for (Locale l : Locale.getAvailableLocales()) {
            System.out.println(l.getDisplayName() + "," + l.getLanguage() + "_" + l.getCountry());
        }
    }
    
    public void loadInstalledLocaleCSVFormatCAPS() {
        for (Locale l : Locale.getAvailableLocales()) {
            System.out.println(l.getDisplayName().toUpperCase() + "," + l.getLanguage() + "_" + l.getCountry());
        }
    }
    
    public void loadInstalledLocaleISOCodesJavaStyle() {
        for (Locale l : Locale.getAvailableLocales()) {
            System.out.println(l.getLanguage() + "_" + l.getCountry() + ",");
        }
    }
    
    public void loadDisplayNamesOnly() {
        for (Locale l : Locale.getAvailableLocales()) {
            System.out.println(l.getDisplayName());
        }
    }
    
    public void loadCountriesOnlyCAPS() {
        for (Locale l : Locale.getAvailableLocales()) {
            System.out.println(l.getDisplayCountry().toUpperCase());
        }
    }
    
    public void loadCountriesOnly() {
        for (Locale l : Locale.getAvailableLocales()) {
            System.out.println(l.getDisplayCountry());
        }
    }
   
}
